package com.example.huadu_server.websocket.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.huadu_server.config.Response;
import com.example.huadu_server.websocket.dto.ConversationDTO;
import com.example.huadu_server.websocket.dto.MessageDTO;
import com.example.huadu_server.websocket.entity.Conversation;
import com.example.huadu_server.websocket.entity.Message;
import com.example.huadu_server.websocket.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    /**
     * 发送消息
     * @param messageDTO 消息内容
     * @return 发送结果的 Response
     */
    @PostMapping("/send")
    public ResponseEntity<Response<Map<String, Object>>> sendMessage(@RequestBody MessageDTO messageDTO) {
        Response<Map<String, Object>> result = chatService.saveMessage(messageDTO);
        return ResponseEntity.status(result.getCode()).body(result);
    }

    /**
     * 获取用户的所有会话列表
     *
     * @param userId 用户 ID
     * @return 包含所有会话的 Response
     */
    @GetMapping("/user/allConversations/{userId}")
    public ResponseEntity<Response<List<ConversationDTO>>> getConversationsByUserId(
            @PathVariable("userId") Integer userId) {
        //校验token
        if (StpUtil.hasPermission("user")) {
            return ResponseEntity.status(401).body(new Response<>(401, "token无效", null));
        }
        Response<List<ConversationDTO>> result = chatService.getConversationsByUserId(userId);
        return ResponseEntity.status(result.getCode()).body(result);
    }

    /**
     * 获取房东的所有会话列表
     *
     * @param landlordId 房东 ID
     * @return 包含所有会话的 Response
     */
    @GetMapping("/landlord/allConversations/{landlordId}")
    public ResponseEntity<Response<List<ConversationDTO>>> getConversationsByLandlordId(
            @PathVariable("landlordId") Integer landlordId) {
        //校验token
        if (StpUtil.hasPermission("landlord")) {
            return ResponseEntity.status(401).body(new Response<>(401, "token无效", null));
        }
        Response<List<ConversationDTO>> result = chatService.getConversationsByLandlordId(landlordId);
        return ResponseEntity.status(result.getCode()).body(result);
    }

    /**
     * 获取会话中的所有消息
     *
     * @param conversationId 会话 ID
     * @return 包含所有消息的 Response
     */
    @GetMapping("/messages/{conversationId}")
    public ResponseEntity<Response<List<Message>>> getMessagesByConversationId(
            @PathVariable Integer conversationId) {

        Response<List<Message>> result = chatService.getMessagesByConversationId(conversationId);
        return ResponseEntity.status(result.getCode()).body(result);
    }

    /**
     * 创建会话
     *
     * @param messageDTO
     * @return
     */
    @PostMapping("/createConversation")
    public ResponseEntity<Response<Conversation>> createConversation(@RequestBody MessageDTO messageDTO) {
        Response<Conversation> result = chatService.createConversation(messageDTO);
        return ResponseEntity.status(result.getCode()).body(result);
    }

    /**
     * 标记指定会话中所有未读消息为已读
     *
     * @param conversationId 会话 ID
     * @return 包含操作结果的 Response
     */
    @PutMapping("/markAsRead/{conversationId}/{viewerId}")
    public ResponseEntity<Response<String>> markMessagesAsRead(
            @PathVariable Integer conversationId,
            @PathVariable Integer viewerId) {
        Response<String> result = chatService.markMessagesAsRead(conversationId, viewerId);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    /**
     * 根据会话ID和用户ID获取该会话中该用户(包括租客和房东)的未读消息数量
     *
     * @param conversationId 会话 ID
     * @param userId         用户 ID
     * @return 未读消息数量的 Response
     */
    @GetMapping("/unReadCount/{conversationId}/{userId}")
    public ResponseEntity<Response<Integer>> getUnreadCountByUserId(
            @PathVariable("conversationId") Integer conversationId,
            @PathVariable("userId") Integer userId) {
        Response<Integer> result = chatService.getUnreadCountByUserId(conversationId, userId);
        return ResponseEntity.status(result.getCode()).body(result);
    }
}
